package mashibing.a_xsb;

import java.util.Arrays;

/**
 * @author yujiafan
 * @version 1.0.0
 * @create 2022-12-22
 */
public class NewClassDay3_01 {

    // 对数器
    public static int[] getIntArray(int maxLength,int maxValue){
        int length = (int) (Math.random() * maxLength);
        int[] arr = new int[length];
        for (int i = 0; i < length; i++) {
            arr[i] = (int) (Math.random() * maxValue);
        }
        Arrays.sort(arr);
        return arr;
    }

    public static int MostLeftIndex(int[] arr,int num){
        if(arr.length < 2){
            return -1;
        }
        int L = 0;
        int R = arr.length - 1;
        int index = -1;
        while (L <= R){
            int mid = (L + R) / 2;
            if(arr[mid] >= num){
                index = mid;
                R = mid - 1;
            } else {
                L = mid + 1;
            }
        }
        return index;
    }

    public static int MostRightIndex(int[] arr,int num){
        if(arr.length < 2){
            return -1;
        }
        int L = 0;
        int R = arr.length - 1;
        int index = -1;
        while (L <= R){
            int mid = (L + R) / 2;
            if(arr[mid] <= num){
                index = mid;
                L = mid + 1;
            } else {
                R = mid - 1;
            }
        }
        return index;
    }

    public static void main(String[] args) {
        int[] arr = getIntArray(50, 20);
        System.out.println(Arrays.toString(arr));
        System.out.println(MostLeftIndex(arr,10));
        System.out.println(MostRightIndex(arr,10));
    }
}
